package com.wx.socket;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

import com.wx.database.DatabaseDao;
import com.wx.entity.User;
import com.wx.util.WeiXinUtil;

/**
 * 服务器端线程处理类
 * @author jinmingwu
 *
 */
public class WXServerThread extends Thread {
	//和本线程相关的Socket
	Socket socket = null;
	public WXServerThread(Socket socket) {
		this.socket = socket;
	}

	//线程执行操作
	@Override
	public void run() {
		// TODO Auto-generated method stub
		super.run();
		InputStream is = null;
		InputStreamReader isr = null;
		BufferedReader br = null;
		OutputStream os = null;
		PrintWriter pw = null ;
		try {
			//获取输入流，并读取客户端信息
			is = this.socket.getInputStream(); //字节输入流
			isr = new InputStreamReader(is); //将字节流转换成字符流
			br = new BufferedReader(isr); //为输入流添加缓冲

			String info = null;
			while((info = br.readLine()) != null) {
				System.out.println("服务器接受到客户端的信息：" + info);
				
				// 传进车牌ID
				int cid = Integer.valueOf(info);
				System.out.println("cid:" + cid);
				int uid = DatabaseDao.cidQueryUid(cid);
				System.out.println("uid:" + uid);
				String carnum = DatabaseDao.cidQueryCarnum(cid);
				System.out.println("carnum:" + carnum);
				try {
					List<User> list = DatabaseDao.uidQueryUser(uid);
					if (list != null && list.size() > 0) {
						WeiXinUtil.initCarTemplateMsg(list.get(0).getUsername(), carnum, new Timestamp(new Date().getTime()).toString(), list.get(0).getOpenid());
					}
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}

			
			socket.shutdownInput();//关闭输入流

			//获取输出流，响应客户端的请求
			os = this.socket.getOutputStream();
			pw = new PrintWriter(os);	//包装为打印流
			pw.write("Welcome!");
			pw.flush();//刷新缓冲
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}	finally {
			//关闭资源
			if(pw != null) {
				pw.close();
			} 
			if(os != null) {
				try {
					os.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if(br != null) {
				try {
					br.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if(isr != null) {
				try {
					isr.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if(is != null) {
				try {
					is.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if(socket != null) {
				try {
					socket.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}

		}


	}
}
